<template>
  <view class="wx-quickly-login">
    <view class="line-left" />
<!--    <text class="wx-quickly-login-text">或</text>
    <view class="line-right" />-->
    <button
      class="wx-quickly-login-btn"
      type="primary"
      open-type="getPhoneNumber"
      @getphonenumber="getPhoneNumber"
    >
      快捷登录
    </button>

    <button
        class="wx-quickly-login-btn wx-quickly-login-cancel-btn"
        type="primary"
        @click="noLogin"
    >
      不登录，继续浏览
    </button>
  </view>
</template>
<script setup>
import { ref, defineProps, getCurrentInstance } from "vue";
import { customNav } from "@/utils/customNav";
import {
  getAccessTokenApi,
  getPhoneNumberApi,
  loginByQuicklyAPi,
} from "@/api/login";
import { onLoad } from "@dcloudio/uni-app";
const props = defineProps({
  agree: {
    type: Boolean,
  },
});

const noLogin = () => {
  uni.switchTab({
    url: "/pages/index/index"
  })
}

const shops_code = uni.getStorageSync("shops_code");
// const shops_code = "1";
const { proxy } = getCurrentInstance();
const phoneCode = ref("");
const userPhoneNumber = ref("");
const getPhoneNumber = async (e) => {
  if (props.agree) {
    if (e.detail?.code) {
      phoneCode.value = e.detail.code;
      getCode();
    }
  } else {
    proxy.modal.msg("请阅读《用户服务协议》和《隐私政策》并勾选");
  }
};

const accessToken = ref("");
const getAccessToken = async () => {
  const res = await getAccessTokenApi();
  const { data } = res;
  accessToken.value = data.access_token;
};

const code = ref("");
const getCode = () => {
  uni.login({
    success(res) {
      code.value = res.code;
      loginByQuickly();
    },
  });
};

/**
 * 小程序手机号一键登录
 * @returns {Promise<void>}
 */
const loginByQuickly = async () => {
  await uni.showLoading({
    title: "登录中...",
  });
  const params = {
    client_id: "huatai",
    client_secret: "waytale",
    auth_type: "wx",
    code: code.value,
    phone_code: phoneCode.value,
    wx_login_type: "0",
    grant_type: "password",
    // username: userPhoneNumber.value
  };
  uni.setStorageSync("token", access_token);
  const res = await loginByQuicklyAPi(params);
  const { access_token } = res;
  await uni.showToast({
    // title: JSON.stringify(res),
    title: "登录成功",
    duration: 2000,
  });
  setTimeout(async () => {
    if (access_token) {
      uni.hideLoading();
      uni.setStorageSync("token", access_token);
	  const isView = uni.getStorageSync("is_view_activity") || ''
    const isViewRefuel = uni.getStorageSync("is_view_refuel") || ''

      if (isView === '1' || isViewRefuel > 0) {
        uni.navigateBack()
      } else if(isView === '2') {
        uni.redirectTo({
          url: "/package_games/pages/nationalDayAnswer",
        });
      } else {
		  if (

			shops_code !== undefined &&
			shops_code !== "" &&
			shops_code !== null
		  ) {
			customNav(`/package_payment/pages/index?shops_code=${shops_code}`);
		  } else {
        /*let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];

        if(prevPage) {
          let prevPageRoute = prevPage.route;
          console.log('上一级页面的路由地址是：' + prevPageRoute);
          if(prevPageRoute === 'package_games/pages/index') {
            uni.navigateBack({
              delta: 1
            })
          } else {
            await uni.switchTab({
              url: "/pages/index/index",
            });
          }
        } else {
          await uni.switchTab({
            url: "/pages/index/index",
          });
        }*/

        await uni.switchTab({
          url: "/pages/index/index",
        });
		  }
	  }

    }
  }, 2000);
};
// onLoad(() => {
//   getAccessToken()
// })
</script>
<style scoped lang=less>
.wx-quickly-login {
  width: 80vw;
  margin-top: 40rpx;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  align-items: center;
  &-text {
    font-weight: 400;
    font-size: 23rpx;
    color: @color-origin-white;
  }
  &-btn {
    width: 80vw;
    margin-top: 29rpx;
    // margin-left: 15rpx;
    // background-image: @login-wx-icon;
    // background-size: 31rpx 31rpx;
    background-color: @color-green;
    background-repeat: no-repeat;
    // background-position: left center;
    font-family: "ali-p";
    font-size: 28rpx;
    font-weight: 400;
    padding-left: 14rpx;
    padding-right: 14rpx;
    border: none;
  }
  .line-left,
  .line-right {
    width: 287rpx;
    height: 1rpx;
    background-color: @color-origin-white;
    opacity: 0.24;
  }
}

button::after {
  border: none;
}

.wx-quickly-login-cancel-btn {
  background-color: #909399;
}
</style>
